package ext;

import java.util.concurrent.CyclicBarrier;

public class CylicTest {
    class FizzBuzz {
        private int n;
        private CyclicBarrier cb;
        public FizzBuzz(int n) {
            this.n = n;
            this.cb = new CyclicBarrier(4);
        }
    
        // printFizz.run() outputs "fizz".
        public void fizz(Runnable printFizz) throws InterruptedException {
            for (int i = 0; i < n; i++) {
                if (i % 3 == 0 && i % 5 != 0) {
                    printFizz.run();
                }
                try {
                    cb.await();
                } catch(Exception e) {
                }
            }
        }
    
        // printBuzz.run() outputs "buzz".
        public void buzz(Runnable printBuzz) throws InterruptedException {
            for (int i = 0; i < n; i++) {
                if (i % 3 != 0 && i % 5 == 0) {
                    printBuzz.run();
                }
                try {
                    cb.await();
                } catch(Exception e) {
                }
            }
        }
    
        // printFizzBuzz.run() outputs "fizzbuzz".
        public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
             for (int i = 0; i < n; i++) {
                if (i % 3 == 0 && i % 5 == 0) {
                    printFizzBuzz.run();
                }
                try {
                    cb.await();
                } catch(Exception e) {
                }
            }       
        }
    
        // // printNumber.accept(x) outputs "x", where x is an integer.
        // public void number(IntConsumer printNumber) throws InterruptedException {
        //     for (int i = 0; i < n; i++) {
        //         if (i % 3 != 0 && i % 5 != 0) {
        //             printNumber.accept(i);
        //         }
        //         try {
        //             cb.await();
        //         } catch(Exception e) {
        //         }
        //     }        
        // }
    }
}
